package com.kgc.sbt.provider;

import com.alibaba.dubbo.config.annotation.Service;
import com.kgc.sbt.api.OrderInfoApi;
import com.kgc.sbt.constant.ActiveMqConstant;
import com.kgc.sbt.domain.OrderInfo;
import com.kgc.sbt.domain.OrderInfoExample;
import com.kgc.sbt.mapper.OrderInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class OrderInfoProvider implements OrderInfoApi {

    @Autowired
    private OrderInfoMapper orderInfoMapper;


    @Override
    public boolean insertOrderInfo(OrderInfo orderInfo) {
        return orderInfoMapper.insert(orderInfo) == 1 ? true : false;
    }

    @Override
    public List<OrderInfo> queryOrderInfoByUserId(Integer userId) {

        OrderInfoExample orderInfoExample = new OrderInfoExample();
        orderInfoExample.setOrderByClause("create_time desc");

        // where 条件
        OrderInfoExample.Criteria criteria = orderInfoExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andIsUserDeleteEqualTo(0);
        //is_user_delete==1为已删除订单

        // 拼接or连接第二个条件
        orderInfoExample.or(criteria);

        // 分页查询
        //orderInfoExample.setOffset(0l);
        //orderInfoExample.setLimit(3);


        return orderInfoMapper.selectByExample(orderInfoExample);
    }

    /**
     * @author Shan
     * @date 2021/6/23
     * @description 逻辑删除
     */
    @Override
    public boolean deleteOrderInfo(Integer orderInfoId) {
        OrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(orderInfoId);
        log.error("---- {} ---", orderInfoId);
        if (orderInfo.getStatusId() == 2 || orderInfo.getStatusId() == 3 || orderInfo.getStatusId() == 4) {
            return false;
        }
        orderInfo.setIsUserDelete(1);
        return orderInfoMapper.updateByPrimaryKey(orderInfo) == 1 ? true : false;
    }

    /**
     * @author Shan
     * @date 2021/6/23
     * @description 修改订单信息
     */
    @Override
    public boolean modOrderStatus(String orderNumber,Integer status) {
        OrderInfo orderInfo = new OrderInfo();
        if (status == 2){
            orderInfo.setStatusId(2);
            orderInfo.setPayTime(new Date());
        }else if (status == 3){
            orderInfo.setStatusId(3);
            orderInfo.setSendTime(new Date());
        }else if (status == 4){
            orderInfo.setStatusId(4);
            orderInfo.setReceiveTime(new Date());
        }
        OrderInfoExample orderInfoExample = new OrderInfoExample();
        OrderInfoExample.Criteria criteria = orderInfoExample.createCriteria();
        criteria.andOrderNumberEqualTo(orderNumber);

        return orderInfoMapper.updateByExampleSelective(orderInfo,orderInfoExample) > 0 ? true : false;
    }

    /**
     * Created on 2021/6/25
     * Author: GongMingJie
     *
     * @param
     * @return Description: 根据订单编号查询订单信息
     */
    @Override
    public OrderInfo getOrderInfoByOrderNumber(String orderNumber) {
        log.info("----- 根据订单号查询用户和价格{}", orderInfoMapper.selectByOrderNumber(orderNumber));
        return orderInfoMapper.selectByOrderNumber(orderNumber);
    }


    /**
     * @author Shan
     * @date 2021/6/30
            * @description 发货
     */
    @Override
    public boolean sendOrder(String orderNumber,String trackNo) {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setTrackNo(trackNo);
        orderInfo.setStatusId(3);
        orderInfo.setSendTime(new Date());
        OrderInfoExample orderInfoExample = new OrderInfoExample();
        OrderInfoExample.Criteria criteria = orderInfoExample.createCriteria();
        criteria.andOrderNumberEqualTo(orderNumber);
        return orderInfoMapper.updateByExampleSelective(orderInfo,orderInfoExample) > 0 ? true : false;
    }
}
